<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="aId">
                    <el-input v-model="formData.aId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="templateId">
                    <el-input v-model="formData.templateId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="caseId">
                    <el-input v-model="formData.caseId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="1-发单用户签署 2-律师签署" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择1-发单用户签署 2-律师签署">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="" prop="userId">
                    <el-input v-model="formData.userId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="areaMobile">
                    <el-input v-model="formData.areaMobile" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="userMobile">
                    <el-input v-model="formData.userMobile" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="fileId">
                    <el-input v-model="formData.fileId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="fileJson">
                    <el-input
                        v-model="formData.fileJson"
                        placeholder="请输入"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="1-已签" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择1-已签">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="生效日期" prop="startDate">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.startDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择生效日期"
                    />
                </el-form-item>
                <el-form-item label="失效日期" prop="endDate">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.endDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择失效日期"
                    />
                </el-form-item>
                <el-form-item label="签署流程id" prop="signFlowId">
                    <el-input v-model="formData.signFlowId" placeholder="请输入签署流程id" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  contractEdit, contractAdd, contractDetail } from '@/api/contract'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑案件合同记录' : '新增案件合同记录'
})

const formData = reactive({
    id: '',
    aId: '',
    templateId: '',
    caseId: '',
    type: '',
    userId: '',
    areaMobile: '',
    userMobile: '',
    fileId: '',
    fileJson: '',
    status: '',
    startDate: '',
    endDate: '',
    signFlowId: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    aId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    templateId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    caseId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择1-发单用户签署 2-律师签署',
            trigger: ['blur']
        }
    ],
    userId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    areaMobile: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    userMobile: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    fileId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    fileJson: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择1-已签',
            trigger: ['blur']
        }
    ],
    startDate: [
        {
            required: true,
            message: '请选择生效日期',
            trigger: ['blur']
        }
    ],
    endDate: [
        {
            required: true,
            message: '请选择失效日期',
            trigger: ['blur']
        }
    ],
    signFlowId: [
        {
            required: true,
            message: '请输入签署流程id',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await contractEdit(data) : await contractAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await contractDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
